package org.nowcoder.huawei.other;

import java.util.Stack;

/**
 * NC52: 有效括号序列
 */

public class NC52 {
    public boolean isValid(String s) {
        Stack<Character> sta = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '{' || c == '[') {
                sta.push(c);
            } else {
                if (sta.isEmpty()) {
                    return false;
                }
                char var = sta.pop();
                if ((var == '(' && c != ')') || (var == '[' && c != ']') || (var == '{' && c != '}')) {
                    return false;
                }
            }
        }
        return sta.isEmpty();
    }
}
